﻿Namespace Commanding
    Public Class FunnelConverter
        Implements IMultiValueConverter

        Private _Values() As Object
        Private _Result As Object = Nothing

        Public Function Convert(values() As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
            Dim result As Object = Nothing

            If values Is Nothing OrElse values.Length = 0 Then
                _Values = Nothing
            Else
                Dim i As Integer = 0
                Do While _Values IsNot Nothing AndAlso (i < _Values.Length And i < values.Length And result Is Nothing)
                    If _Values(i) Is Nothing OrElse Not _Values(i).Equals(values(i)) Then
                        result = values(i)
                    End If
                    i += 1
                Loop
                Do While result Is Nothing And i < values.Length
                    result = values(i)
                    i += 1
                Loop
                _Values = values.ToArray
            End If

            If result IsNot Nothing Then
                _Result = result
            End If
            Return _Result

        End Function

        Public Function ConvertBack(value As Object, targetTypes() As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
            Throw New NotSupportedException
        End Function
    End Class
End Namespace
